﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedResources.Tools;

namespace System
{
	public static class SimpleTypeExtensions
	{
		public static byte[] ToByteArray(this ushort @this)
		{
			var buffer = new byte[2];
			buffer[0] = (byte)(@this & 0x00FF);
			buffer[1] = (byte)((@this & 0xFF00)>>8);
			return buffer;
		}

		public static byte[] ToByteArray(this uint @this)
		{
			var buffer = new byte[4];
			buffer[0] = (byte)(@this & 0x000000FF);
			buffer[1] = (byte)((@this & 0x0000FF00) >> 8);
			buffer[2] = (byte)((@this & 0x00FF0000) >> 16);
			buffer[3] = (byte)((@this & 0xFF000000) >> 24);
			return buffer;
		}

		private static TENUM ConvertToEnumInternal<TENUM>(object value, TENUM unknownValue)
		{
			TENUM destValue;
			value.TryConvertToType<TENUM>(out destValue, unknownValue);
			return destValue;
		}

		public static TENUM AsEnum<TENUM>(this byte value, TENUM unknownValue)
		{
			return ConvertToEnumInternal(value, unknownValue);
		}

		public static TENUM AsEnum<TENUM>(this ushort value, TENUM unknownValue)
		{
			return ConvertToEnumInternal(value, unknownValue);
		}

		public static TENUM AsEnum<TENUM>(this int value, TENUM unknownValue)
		{
			return ConvertToEnumInternal(value, unknownValue);
		}

		public static TENUM AsEnum<TENUM>(this uint value, TENUM unknownValue)
		{
			return ConvertToEnumInternal(value, unknownValue);
		}

		public static TENUM AsEnum<TENUM>(this long value, TENUM unknownValue)
		{
			return ConvertToEnumInternal(value, unknownValue);
		}

		public static TENUM AsEnum<TENUM>(this ulong value, TENUM unknownValue)
		{
			return ConvertToEnumInternal(value, unknownValue);
		}

	}
}
